<!DOCTYPE html>
<html>
	<head>
	    <meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
		<link rel="stylesheet" href="$!rc.contextPath/common/bootstrap.min.css" />
		<link rel="stylesheet" href="$!rc.contextPath/common/common.css" />
		<link rel="stylesheet" href="//at.alicdn.com/t/font_604979_156g2g4k47w7mn29.css" />
		<link rel="stylesheet" href="$!rc.contextPath/common/dropload.css" />
		<link rel="stylesheet" href="$!rc.contextPath/css/index.css">
		<link rel="stylesheet" href="$!rc.contextPath/css/shops_list.css" />
		<title>商品列表</title>
	</head>
	<body>
	<div class="title text-center">
		<a onClick="javascript:window.history.go(-1)"><</a>
		搜索结果
	</div>
	<div class="tab-list">
		<div class="tab-item choosed" onclick="f_order(1)">
			<span>综合</span>
		</div>
		<div class="tab-item" onclick="f_order(2)">
			<span>销量</span>
		</div>
		<div class="tab-item orderby-price" onclick="f_order(3)">
			<span>价格</span>
			<i class="iconfont icon-jiantou1"></i>
		</div>
	</div>
	<div class="floor2 content">
		<div class="cart-list lists">
			
		</div>
	</div>
	</body>
	<script src="$!rc.contextPath/common/jquery-1.8.3.min.js"></script>
	<script src="$!rc.contextPath/common/dropload.min.js"></script>
	<script src="$!rc.contextPath/js/shops_list.js"></script>
	<script type="text/javascript">
	var orderType = 1;
	var keyword = '$!keyword';
	$(function(){
		f_init();
	});
	
	function f_init(){
		$(".lists").empty();
		$(".dropload-down").remove();
		
		var counter = 0;
	    // 每页展示4个
	    var num = 5;
	    var pageStart = 0,pageEnd = 0;
	
	    // dropload
	    $('.content').dropload({
	        scrollArea : window,
	        domUp : {
	            domClass   : 'dropload-up',
	            domRefresh : '<div class="dropload-refresh">↓下拉刷新</div>',
	            domUpdate  : '<div class="dropload-update">↑释放更新</div>',
	            domLoad    : '<div class="dropload-load"><span class="loading"></span>加载中...</div>'
	        },
	        domDown : {
	            domClass   : 'dropload-down',
	            domRefresh : '<div class="dropload-refresh">↑上拉加载更多</div>',
	            domLoad    : '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
	            domNoData  : '<div class="dropload-noData">~没有更多商品了~</div>'
	        },
	        loadDownFn : function(me){
	            $.ajax({
	                type: 'POST',
	                url: '$!rc.contextPath/sku/listByKeyword',
	                data : {
	                	keyword : keyword,
	                	startIndex : pageStart,
	                	pageSize : num,
	                	orderType : orderType
	                },
	                dataType: 'json',
	                success: function(data){
	                    var result = '';
	                    counter++;
	                    pageEnd = num * counter;
	                    pageStart += num;
	                    if(data.code == '0'){
	                    	var list = data.data;
	                    	for(var i = 0; i < list.length; i++){
		                        result += '<div class="cart-item" ><div class="cart-img col-xs-4" onclick="f_detail(' + list[i].sku_id + ')"><img src="http://app.tmaipay.com/' + list[i].attach_path + '"/></div><div class="cart-detail col-xs-8" onclick="f_detail(' + list[i].sku_id + ')">' + 
		                        			'<div class="cart-title">' + list[i].sku_name + '</div><div><div class="price clear text-left"><div class="price-right"><div class="yen">' + 
		                        			'￥</div><div class="price-num">' + list[i].promotion_price + '</div></div></div></div></div><div class="buy" onclick="f_add_shopcart(' + list[i].sku_id + ')"><i class="iconfont icon-ziyuan"></i></div></div>';
		                        
	                    	}
	                    	
	                    	if(list.length < 5){
	                            // 再往下已经没有数据
	                            // 锁定
	                            me.lock();
	                            // 显示无数据
	                            me.noData();
	                            //break;
	                        }
	                    }
	
                        $('.lists').append(result);
                        // 每次数据加载完，必须重置
                        me.resetload();
	                },
	                error: function(xhr, type){
	                    alert('网络故障!');
	                    // 即使加载出错，也得重置
	                    me.resetload();
	                }
	            });
	        },
	        threshold : 50
	    });
	}
	
	function f_order(type){
		if(type == 3){
			if($(".icon-jiantou1").attr("class").indexOf("up") == -1){
				type = 3;
			}else{
				type = 4;
			}
		}
		orderType = type;
		
		f_init();
	}
	
	function f_detail(skuId){
		window.location.href = '$!rc.contextPath/skuView/detail?skuId=' + skuId;
	}
	
	function f_add_shopcart(skuId){
		
		$.ajax({
            type: 'POST',
            url: '$!rc.contextPath/sku/addShopCart',
            data : {
            	skuId : skuId
            },
            dataType: 'json',
            success: function(data){
                console.log(data);
                if(data.code == '0'){
                	alert("已加入购物车！")
                } else if(data.code == '-2'){
                	window.location.href = '$!rc.contextPath/memberView/bindMobile';
                } else{
                	alert(data.text)
                }
            }
        });
		
	}
	</script>
</html>
